/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.hazard.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.close.basic.service.ClosedOperateAuditRecordService;
import com.hopes.bizz.close.common.constant.CloseModuleEnums;
import com.hopes.bizz.close.common.constant.HazardDicConstant;
import com.hopes.bizz.close.hazard.dto.ClosedHazardCarAuditDTO;
import com.hopes.bizz.close.hazard.dto.ClosedHazardCarDTO;
import com.hopes.bizz.close.hazard.entity.ClosedHazardCarEntity;
import com.hopes.bizz.close.hazard.dto.ClosedHazardCarExcelDTO;
import com.hopes.bizz.close.hazard.vo.ClosedHazardCarExcelVO;
import com.hopes.bizz.close.hazard.mapper.ClosedHazardCarMapper;
import com.hopes.bizz.close.hazard.service.ClosedHazardCarService;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import com.hopes.boss.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 危险品运输车辆
 *
 * @author pig
 * @date 2023-10-24 19:40:58
 */
@Service
@RequiredArgsConstructor
public class ClosedHazardCarServiceImpl extends ServiceImpl<ClosedHazardCarMapper, ClosedHazardCarEntity> implements ClosedHazardCarService {

    private final ClosedOperateAuditRecordService closedOperateAuditRecordService;


    @Override
    public boolean save(ClosedHazardCarEntity entity) {
        entity.setApplyUserId(SecurityUtils.getUserId());
        if (ObjUtil.isNull(entity.getApplyTime())) {
            entity.setApplyTime(Calendar.getInstance().getTime());
        }
        entity.setAuditStatus(HazardDicConstant.CLOSE_HAZARD_CAR_AUDIT_STATUS_NOT_SUBMIT);
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitById(Long id) {
        // 1、修改状态
        lambdaUpdate()
                .set(ClosedHazardCarEntity::getAuditStatus, HazardDicConstant.CLOSE_HAZARD_CAR_AUDIT_STATUS_NOT_AUDIT)
                .eq(ClosedHazardCarEntity::getId, id).update();
        // 2、创建提交日志记录
        return closedOperateAuditRecordService.buildSaveOperateAuditRecord(id,
                CloseModuleEnums.GeneralCarOperateEnum.submit.getValue(),
                HazardDicConstant.CLOSE_HAZARD_CAR_AUDIT_STATUS_NOT_AUDIT, null, null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAndSubmit(ClosedHazardCarEntity closedHazardCar) {
        // 1、保存危险品车辆申请
        closedHazardCar.setApplyUserId(SecurityUtils.getUserId());
        if (ObjUtil.isNull(closedHazardCar.getApplyTime())) {
            closedHazardCar.setApplyTime(Calendar.getInstance().getTime());
        }
        closedHazardCar.setAuditStatus(HazardDicConstant.CLOSE_FLOW_BILL_AUDIT_STATUS_NOT_AUDIT);
        // 1、重新的了save方法，不可使用saveOrUpdate方法
        if (ObjUtil.isNull(closedHazardCar.getId())) {
            getBaseMapper().insert(closedHazardCar);
        } else {
            getBaseMapper().updateById(closedHazardCar);
        }
        // 2、创建 && 提交日志
        return closedOperateAuditRecordService.buildSaveOperateAuditRecord(closedHazardCar.getId(),
                CloseModuleEnums.GeneralCarOperateEnum.audit.getValue(),
                HazardDicConstant.CLOSE_HAZARD_CAR_AUDIT_STATUS_NOT_AUDIT, null, null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean auditById(ClosedHazardCarAuditDTO closedGeneralCarAuditDTO) {
        // 1、修改对象
        lambdaUpdate()
                .set(ClosedHazardCarEntity::getAuditStatus, closedGeneralCarAuditDTO.getAuditStatus())
                .set(ClosedHazardCarEntity::getAuditStatusDesc, closedGeneralCarAuditDTO.getAuditStatusDesc())
                .set(ClosedHazardCarEntity::getAuditTime, Calendar.getInstance().getTime())
                .eq(ClosedHazardCarEntity::getId, closedGeneralCarAuditDTO.getId()).update();
        // 2、创建 && 提交日志
        return closedOperateAuditRecordService.buildSaveOperateAuditRecord(closedGeneralCarAuditDTO.getId(),
                CloseModuleEnums.GeneralCarOperateEnum.audit.getValue(),
                closedGeneralCarAuditDTO.getAuditStatus(), closedGeneralCarAuditDTO.getAuditStatusDesc(), closedGeneralCarAuditDTO.getRemark());
    }

    @Override
    public IPage<ClosedHazardCarEntity> queryAppHazardCarSelfPage(Page page, ClosedHazardCarDTO closedHazardCarDTO) {
        final LambdaQueryWrapper<ClosedHazardCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ClosedHazardCarEntity::getApplyUserId, SecurityUtils.getUserId());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getOrgId()), ClosedHazardCarEntity::getOrgId, closedHazardCarDTO.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getUseFlag()), ClosedHazardCarEntity::getUseFlag, closedHazardCarDTO.getUseFlag());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getHazardType()), ClosedHazardCarEntity::getHazardType, closedHazardCarDTO.getHazardType());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getAuditStatus()), ClosedHazardCarEntity::getAuditStatus, closedHazardCarDTO.getAuditStatus());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getBlacklistFlag()), ClosedHazardCarEntity::getBlacklistFlag, closedHazardCarDTO.getBlacklistFlag());
        wrapper.like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getLicencePlateNumber, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverName, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverPhone, closedHazardCarDTO.getKeyword());
        wrapper.orderByDesc(ClosedHazardCarEntity::getApplyTime);
        return this.page(page, wrapper);
    }

    @Override
    public IPage<ClosedHazardCarEntity> queryAppHazardCarAuditPage(Page page, List<Integer> auditStatusList, ClosedHazardCarDTO closedHazardCarDTO) {
        final LambdaQueryWrapper<ClosedHazardCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollUtil.isNotEmpty(auditStatusList), ClosedHazardCarEntity::getAuditStatus, auditStatusList);
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getOrgId()), ClosedHazardCarEntity::getOrgId, closedHazardCarDTO.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getUseFlag()), ClosedHazardCarEntity::getUseFlag, closedHazardCarDTO.getUseFlag());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getHazardType()), ClosedHazardCarEntity::getHazardType, closedHazardCarDTO.getHazardType());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getAuditStatus()), ClosedHazardCarEntity::getAuditStatus, closedHazardCarDTO.getAuditStatus());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getBlacklistFlag()), ClosedHazardCarEntity::getBlacklistFlag, closedHazardCarDTO.getBlacklistFlag());
        wrapper.like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getLicencePlateNumber, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverName, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverPhone, closedHazardCarDTO.getKeyword());
        wrapper.orderByDesc(ClosedHazardCarEntity::getApplyTime);
        return this.page(page, wrapper);
    }

    @Override
    public ValidateImportBO<ClosedHazardCarExcelDTO> validateImport(List<ClosedHazardCarExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedHazardCarExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedHazardCarExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedHazardCarEntity importDtoToEntity(ClosedHazardCarExcelDTO excelDto) {
        ClosedHazardCarEntity entity= new ClosedHazardCarEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public ClosedHazardCarExcelVO exportEntityToVo(ClosedHazardCarEntity entity) {
        ClosedHazardCarExcelVO excelVO = new ClosedHazardCarExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }


}
